<?php
declare (strict_types=1);

namespace app\exam\model;

use app\common\model\{BaseModel, ProductType};
use think\model\concern\SoftDelete;


/**
 * 专项版块模型
 */
class Special extends BaseModel
{
    use softDelete;
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'exam_module_special';

    /**
     * 定义字段类型
     * @var [type]
     */
    protected $type = [
        'module_id'     => 'intger',

    ];

    protected $globalScope  = ['status'];
    protected $append       = ['user_price']; // 根据用户身份计算的价格

    /**
     * 状态条件限制
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-02-04
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    public function scopeStatus($query)
    {
    	$query->where('status', 1);
    }

    /**
     * 根据用户身份返回价格
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date     21/03/29
     * @param string $value
     * @param $data
     * @return float
     */
    public function getUserPriceAttr($value = '', $data): float
    {
        return self::getUserPrice($data);
    }

    /**
     * 专项是否购买
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-01
     */
    public function HasBought()
    {
        return $this->hasOne(OrderItem::class, 'product_id')
        ->whereProductTypeId(ProductType::getProductIdByKey('special'))
        ->join('payment_order po', "po.order_no=" . env('database.prefix', '') . "payment_order_item.order_no AND po.user_id=" . request()->mid )
        ->where('status', 'IN', '20,30,40');
    }

    /**
     * 关联机构
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-08
     * @return [type] [description]
     */
    public function mhmId()
    {
        return $this->belongsTo(Module::class, 'module_id')->bind(['mhm_id']);
    }

    /**
     * 关联正确率
     * @return [type] [description]
     */
    public function accuracy()
    {
        return $this->belongsTo(TopicSpecialRecord::class, 'id', 'special_id')->bind(['accuracy']);
    }
}
